<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class jalan extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->helper('flexigrid');
        $this->load->library('flexigrid');
		$this->load->library('session');
		$this->load->library('form_validation');
        $this->load->model('jalan_model');
        $this->load->model('ustad_model');
        $this->load->model('periode_model');
        $this->load->model('kelas_santri_model');
    }

//end Main
    //load Grid
    function index() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			$colModel['no'] = array('No', 30, TRUE, 'center', 0);
            $colModel['NO_RUAS'] = array('No Ruas Jalan', 100, TRUE, 'center', 1);
            $colModel['NAMA_RUAS_JALAN'] = array('Nama Ruas Jalan', 200, TRUE, 'center', 1);
            $colModel['PANJANG'] = array('Panjang', 150, TRUE, 'center', 1);
            $colModel['STATUS'] = array('Status', 100, TRUE, 'center', 1);
            $colModel['Edit'] = array('Edit', 30, TRUE, 'center', 0);
			
			
			$gridParams = array(
                'width' => 'auto',
                'height' => 350,
                'rp' => 10,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Master Ruas Jalan',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
            $buttons[] = array('Hapus', 'delete', 'spt_js');
		

            // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("jalan/grid/"), $colModel, 'NO_RUAS', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
		function spt_js(com,grid)
		{
			if (com=='Tambah')
			{
				location.href='" . base_url() . "index.php/jalan/form';
			}
			
			if (com=='Kembali ke daftar periode')
			{
				location.href='" . base_url() . "index.php/periode';
			}

			if (com=='Hapus')
				{
				   if($('.trSelected',grid).length>0){
					   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' jalan?')){
							var items = $('.trSelected',grid);
							var itemlist ='';
							for(i=0;i<items.length;i++){
								itemlist+= items[i].id.substr(3)+',';
							}
							$.ajax({
							   type: 'POST',
							   url: '" . base_url() . "index.php/jalan/delete" . "',
							   data: 'items='+itemlist,
							   success: function(data){
								$('#flex1').flexReload();
							   }
							});
						}
					} else {
						return false;
					}
				}

		}
		</script>
		";


            $data['js_grid'] = $grid_js;

            //rendering view
            //$data['title'] = 'Daftar Peserta';
            //$data['js_grid']=$grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid() {
        $valid_fields = array('NO_RUAS', 'NAMA_RUAS_JALAN', 'PANJANG');
        $this->flexigrid->validate_post('NO_RUAS', 'asc', $valid_fields);
        $records = $this->jalan_model->get_data();
        $this->output->set_header($this->config->item('json_header'));

        $no = 0;
        foreach ($records['records']->result() as $row) {
			if($row->STATUS == 1){
				$status = 'Aktif';
			}
			else $status = 'Tidak Aktif';
			
            $record_items[] = array(
                $row->NO_RUAS,
                $no = $no + 1,
                $row->NO_RUAS,
				$row->NAMA_RUAS_JALAN,
                number_format($row->PANJANG, null, ',', '.').' meter',
				$status,
                '<a href=\'' . base_url() . 'index.php/jalan/edit/' . $row->NO_RUAS. '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> '
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }

    function form(){
        if ($this->session->userdata('username') == NULL){
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			//$data['ustad'] = $this->ustad_model->get_data_all_ustad();
            $data['content'] = $this->load->view('form_jalan', $data, true);
            $this->load->view('main', $data);
        }
    }

    function add($id_periode) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() == false)
			{
				$this->form();
			}
			else{
				$data = array(
					'NO_RUAS' => $this->input->post('no_ruas'),
					'NAMA_RUAS_JALAN' => $this->input->post('nama_ruas'),
					'PANJANG' => $this->input->post('panjang'),
					'GPS_AWAL_LAT' => $this->input->post('gps_awal_lat'),
					'GPS_AWAL_LONG' => $this->input->post('gps_awal_long'),
					'GPS_AKHIR_LAT' => $this->input->post('gps_akhir_lat'),
					'GPS_AKHIR_LONG' => $this->input->post('gps_akhir_long'),
					'ID_PERIODE' => $id_periode
				);
				$data = str_replace("'", "\'", $data);
				$this->jalan_model->insert($data);
				redirect('jalan/index/'.$id_periode.'');
			}
        }
    }

	
    public function edit($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->jalan_model->selectone($id);
            foreach ($record_artikel->result() as $artikel) {
                $data['no_ruas'] = $artikel->NO_RUAS;
                $data['nama_ruas'] = $artikel->NAMA_RUAS_JALAN;
                $data['panjang'] = $artikel->PANJANG;
                $data['gps_awal_lat'] = $artikel->GPS_AWAL_LAT;
                $data['gps_awal_long'] = $artikel->GPS_AWAL_LONG;
                $data['gps_akhir_lat'] = $artikel->GPS_AKHIR_LAT;
                $data['gps_akhir_long'] = $artikel->GPS_AKHIR_LONG;
                $data['panjang'] = $artikel->PANJANG;
            }
            //$data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data['content'] = $this->load->view('form_jalan', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() == false)
				{
					$this->edit($id);
				}
				else{
					$data = array(
						'NO_RUAS' => $this->input->post('no_ruas'),
						'NAMA_RUAS_JALAN' => $this->input->post('nama_ruas'),
						'PANJANG' => $this->input->post('panjang'),
						'GPS_AWAL_LAT' => $this->input->post('gps_awal_lat'),
						'GPS_AWAL_LONG' => $this->input->post('gps_awal_long'),
						'GPS_AKHIR_LAT' => $this->input->post('gps_akhir_lat'),
						'GPS_AKHIR_LONG' => $this->input->post('gps_akhir_long')
					);
					$this->jalan_model->update($id, $data);
					redirect('jalan/index/'.$this->session->userdata('id_periode').'');
				}
        }
    }

    
    function delete() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
                    $this->jalan_model->delete($id);
                }//end if
            }//end foreach
        }
    }
	
	
	function cek_validasi() {
        // Setting Rules
        $this->form_validation->set_rules('no_ruas', 'No Ruas Jalan', 'required|numeric|greater_than[0]');
        $this->form_validation->set_rules('nama_ruas', 'Nama Ruas Jalan', 'required');
        $this->form_validation->set_rules('panjang', 'Panjang Jalan', 'required|numeric|greater_than[0]');
        //$this->form_validation->set_rules('tgl_selesai', 'Tanggal Selesai', 'required');
		
		
        //Setting Error Message
        $this->form_validation->set_message('required', 'Field %s harus diisi.');
        $this->form_validation->set_message('numeric', 'Field %s harus diisi angka.');
		$this->form_validation->set_message('greater_than', 'Field %s harus diisi angka lebih dari 0');


        // Setting Delimiter
        $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }

}

//end class

/* End of file welcome.php */
/* Location: ./system/application/controllers/main.php */
